var krepoList = "#krepoList", krepName = "#krepName", krepType = "#krepType", kform = "#kform", krepoDb = ".krepoDb", krepoFile = ".krepoFile";
var keditRepId = "#keditRepId", krepPageNum = "#krepPageNum", krepoDbList = "#krepoDbList";
var krepoIds = {
	"krepoFile": [{"type": "text", "id": "krepBasePath"}], 
	"krepoDb": [
		{"type": "combo", "id": "kdbType"}, {"type": "combo", "id": "kdbAccess"}, {"type": "text", "id": "kdbHost"}, {"type": "text", "id": "kdbPort"},
		{"type": "text", "id": "kdbName"}, {"type": "text", "id": "kdbUsername"}, {"type": "text", "id": "kdbPassword"}
	]
};

function krepoListPage(param) {
	param.pgCt = param.page;
	param.pgSz = param.rows;
	delete param.page;
	delete param.rows;
	return param;
}

function krepoDatagridLoader(param, success, error) {
	let trepName = $(krepName).textbox("getValue");
	let trepType = $(krepType).combobox("getValue");
	if ("" != $.trim(trepName)) param.repName = trepName;
	if ("" != $.trim(trepType)) param.repType = trepType;
	
	ajaxPost(reqRoute.repositoryList, krepoListPage(param), function(resultData) {
		success(resultData);
	});
}

function krepoSearch() {
	$(krepoList).datagrid('load', {});
}

function krepoReset() {
	$(krepName).textbox('clear');
	$(krepType).combobox("clear");
	krepoSearch();
}

function krepoAddPage() {
	$(kcenter).panel({ href: pageRoute.repoAdd });
}

function krepoEditPage(id) {
	var krepIdVal = $(keditRepId).val();
	if (krepIdVal) {
		$(keditRepId).remove();
	}
	$("body").append("<input id='"+keditRepId.substr(1)+"' value='"+id+"'>");
	$(kcenter).panel({ href: pageRoute.repoEdit });
}

function krepoDbPage(id, repName) {
	// 获取当前页码
	var currentPage = $(krepoList).datagrid('options').pageNumber;
	// 设置
	var pageNum = $(krepPageNum).val();
	if (pageNum) {
		$(krepPageNum).remove();
	}
	$("body").append("<input id='"+krepPageNum.substr(1)+"' value='"+id+","+repName+","+currentPage+"'>");
	$(kcenter).panel({ href: pageRoute.repoDbList });
}

function krepoDelete(id, repName) {
	$.messager.confirm('提示', '确定要删除[' + repName + ']资源库吗？', function(r) {
	    if (r) {
	    	$.get(reqRoute.repositoryDel, { id: id }, function(resultData) {
	    		if (resultData.status == successCode) {
	    			krepoSearch();
	    		} else {
	    			$.messager.alert("提示", resultData.message, "error");
	    		}
	    	});
	    }
	});
}

function krepoSetRequired(record) {
	var type = record.value;
	var fileData = krepoIds.krepoFile;
	var dbData = krepoIds.krepoDb;
	if ("file" == type) {
		$(krepoDb).hide();
		$(krepoFile).show();
	} else {
		$(krepoDb).show();
		$(krepoFile).hide();
	}
	$(fileData).each(function(index, item) {
		var bool = ("file" == type ? true : false);
		if ("combo" == item.type) {
			$("#"+item.id).combobox("options").required = bool;
			$("#"+item.id).combobox("textbox").validatebox("options").required = bool;
			$("#"+item.id).combobox("validate");
		} else if ("text" == item.type) {
			$("#"+item.id).textbox("options").required = bool;
			$("#"+item.id).textbox("textbox").validatebox("options").required = bool;
			$("#"+item.id).textbox("validate");
		}
	});
	$(dbData).each(function(index, item) {
		var bool = ("db" == type ? true : false);
		if ("combo" == item.type) {
			$("#"+item.id).combobox("options").required = bool;
			$("#"+item.id).combobox("textbox").validatebox("options").required = bool;
			$("#"+item.id).combobox("validate");
		} else if ("text" == item.type) {
			$("#"+item.id).textbox("options").required = bool;
			$("#"+item.id).textbox("textbox").validatebox("options").required = bool;
			$("#"+item.id).textbox("validate");
		}
	});
	$(kform).form('disableValidation');
}

function krepoTest() {
	if ($(kform).form('enableValidation').form('validate')) {
		ajaxPost(reqRoute.repositoryTestConn, formData(kform), function(resultData) {
			if (resultData.status == successCode) {
				$.messager.alert({ title: '提示', msg: resultData.message, icon: 'info' });
			} else {
				$.messager.alert({ title: '提示', msg: resultData.message, icon: 'error' });
			}
		});
	}
}

function krepoSubmit(bool) {
	var turl = (bool? reqRoute.repositoryAdd : reqRoute.repositoryEdit);
	if ($(kform).form('enableValidation').form('validate')) {
		ajaxPost(turl, formData(kform), function(resultData) {
			if (resultData.status == successCode) {
				$(kcenter).panel({ href: pageRoute.repoList });
			} else {
				$.messager.alert({ title: '提示', msg: resultData.message, icon: 'error' });
			}
		});
	}
}

function krepoClear() {
	$(kcenter).panel({ href: pageRoute.repoList });
}

function krepoDbShowPwd(pwd) {
	$.get(reqRoute.showPwd, { pwd: pwd }, function(resultData) {
		resultData = JSON.parse(resultData);
		if (resultData.status == successCode) {
			$.messager.alert("提示", resultData.data, "info");
		} else {
			$.messager.alert("提示", resultData.message, "error");
		}
	})
}

function krepoSharedDb(name) {
	var paramData = $(krepPageNum).val().split(',');
	$.get(reqRoute.sharedDb, { repId: paramData[0], name: name }, function(resultData) {
		if (resultData.status == successCode) {
			$.messager.alert("提示", "共享成功", "info");
			$(krepoDbList).datagrid("load", {});
		} else {
			$.messager.alert("提示", resultData.message, "error");
		}
	});
}

function krepoCloseSharedDb(name) {
	$.get(reqRoute.closeSharedDb, { name: name }, function(resultData) {
		if (resultData.status == successCode) {
			$(krepoDbList).datagrid("load", {});
			$.messager.alert("提示", "共享成功", "info");
		} else {
			$.messager.alert("提示", resultData.message, "error");
		}
	});
}